package com.ynu.springbootappdemo.controller;


import com.ynu.springbootappdemo.common.utils.BaseResponse;
import com.ynu.springbootappdemo.common.utils.ResultUtils;
import com.ynu.springbootappdemo.model.dto.orders.SaveOrderDTO;
import com.ynu.springbootappdemo.model.dto.orders.UpdateOrderStateDTO;
import com.ynu.springbootappdemo.model.entity.Orders;
import com.ynu.springbootappdemo.model.entity.Properties;
import com.ynu.springbootappdemo.model.entity.SkuItem;
import com.ynu.springbootappdemo.model.vo.DeliveryAddressVo;
import com.ynu.springbootappdemo.model.vo.OrderDetailsVo;
import com.ynu.springbootappdemo.model.vo.OrderVo;
import com.ynu.springbootappdemo.model.vo.SkuItemVo;
import com.ynu.springbootappdemo.service.GoodsService;
import com.ynu.springbootappdemo.service.OrderService;
import com.ynu.springbootappdemo.service.WebSocketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Api(tags = "订单")
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Resource
    private OrderService service;

    @Resource
    private GoodsService goodsService;

    @Resource
    private ModelMapper modelMapper;

    @Resource
    private WebSocketService webSocketService;

    @PostMapping("/save")
    @ApiOperation("添加订单")
    @Transactional
    public BaseResponse<OrderVo> addOrder(@ApiParam("订单信息") @RequestBody SaveOrderDTO dto
            , HttpServletRequest request) {
        Orders saveOrder = service.saveOrder(dto, request);
        OrderVo vo = OrdersParserToVo(saveOrder);
        return  ResultUtils.success(vo);
    }

    @GetMapping("getById/{id}")
    @ApiOperation("根据id获取订单详情")
    @Transactional
    public BaseResponse<OrderVo> getOrderById(@PathVariable("id") Long id,HttpServletRequest request){
        Orders orders = service.getById(id, request);
        OrderVo vo = OrdersParserToVo(orders);
        return  ResultUtils.success(vo);
    }

    @GetMapping("/getAll")
    @ApiOperation("获取当前登录用户的全部订单")
    @Transactional
    public BaseResponse<List<OrderVo>> getAllOrder(HttpServletRequest request){
        List<Orders> ordersList = service.getAllOrders(request);
        List<OrderVo> vos = new ArrayList<>();
        for (Orders orders : ordersList) {
            vos.add(OrdersParserToVo(orders));
        }
        return ResultUtils.success(vos);
    }

    @PostMapping("/updateState")
    @ApiOperation("支付成功后修改订单状态")
    @Transactional
    public BaseResponse<OrderVo> updateState(@ApiParam("订单信息") @RequestBody UpdateOrderStateDTO dto , HttpServletRequest request){
        Orders orders = service.updateState(dto, request);
        OrderVo vo = OrdersParserToVo(orders);
        return ResultUtils.success(vo);
    }

    @PostMapping("/confirmOrder")
    @ApiOperation("确认订单")
    @Transactional
    public BaseResponse<OrderVo> confirmOrder(@ApiParam("订单信息") @RequestBody UpdateOrderStateDTO dto , HttpServletRequest request){
        Orders orders = service.updateState(dto, request);
        OrderVo vo = OrdersParserToVo(orders);
        return ResultUtils.success(vo);
    }

    private OrderVo OrdersParserToVo(Orders orders){
        // 转换为VO
        OrderVo vo = modelMapper.map(orders, OrderVo.class);

        // 转换地址Vo
        DeliveryAddressVo addressVo = modelMapper.map(orders.getDeliveryAddress(), DeliveryAddressVo.class);
        vo.setDeliveryAddress(addressVo);

        // 转换OrderDetailVo
        Set<OrderDetailsVo> orderDetailsVos = new HashSet<>();
        orders.getOrderDetails().forEach(orderDetail ->{
            //转换sku
            SkuItem skuItem = orderDetail.getSkuItem();
            List<Properties> properties = goodsService.parserProp(skuItem.getItemSpec());

            //转换属性
            SkuItemVo skuItemVo = modelMapper.map(skuItem, SkuItemVo.class);
            skuItemVo.setItemSpec(properties);

            // 转换每一个订单细则
            OrderDetailsVo detailsVo = modelMapper.map(orderDetail, OrderDetailsVo.class);
            detailsVo.setSkuItemVo(skuItemVo);

            orderDetailsVos.add(detailsVo);
        });
        vo.setDetailsVos(orderDetailsVos);
        return vo;
    }


}
